package com.wanmao.test;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanmao.test.adapter.DefectDetailAdapter;
import com.wanmao.test.model.DefectModel;
import com.wanmao.test.service.ToolShowDemo;
import com.wanmao.test.util.DialogUtils;
import com.wanmao.test.util.StaticUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wanmao.test.util.DateUtils.toTimeStr2;



public class DefectDetailActivity extends BaseActivity {
    private String TAG="缺陷详情";
    private ListView tableLv;
    private String OBJ_ID,type;
    private List<DefectModel> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_defect_detail);
        list=new ArrayList<>();
        tableLv = (ListView) findViewById(R.id.lv);
        OBJ_ID = getIntent().getStringExtra("OBJ_ID");
        type = getIntent().getStringExtra("type");
        MyAsynTask myAsynTask = new MyAsynTask();
        myAsynTask.execute();
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void initDataSD(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("ISC_ID", StaticUtil.ISC_ID);
        map.put("APPID", StaticUtil.APPID);
        map.put("OBJ_ID", OBJ_ID);
        String response = ToolShowDemo.manageWebServices("GetQXXXXX", map);
        Log.e(TAG, "输电 response："+response);
        JSONObject json = JSON.parseObject(response);

        checkParams(json);

    }
    public void initDataBD(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("ISC_ID", StaticUtil.ISC_ID);
        map.put("APPID", StaticUtil.APPID);
        map.put("QXID", OBJ_ID);
        String response = ToolShowDemo.manageWebServices("GetSBQXXXXX", map);
        Log.e(TAG, "变电 response："+response);
        JSONObject json = JSON.parseObject(response);
        checkParams(json);
    }
    private void checkParams(JSONObject json){
        int result = json.getIntValue("result");
        if (result == 0) {
            String content = json.getString("description");
        } else {
            JSONArray value = json.getJSONArray("resultValue");
            if (value.size() <= 0) {

            } else {
                JSONObject obj = value.getJSONObject(0);
                if (obj != null) {
                    list.add(new DefectModel("缺陷主设备", obj.getString("QXZSB")));
                    list.add(new DefectModel("电站/线路名", obj.getString("DZORXLMC")));
                    list.add(new DefectModel("站线类型", obj.getString("ZXLX")));
                    list.add(new DefectModel("电压等级", obj.getString("DYDJ")));
                    list.add(new DefectModel("设备类型", obj.getString("SBLX")));
                    list.add(new DefectModel("设备种类", obj.getString("SBZL")));
                    list.add(new DefectModel("设备型号", obj.getString("SBXH")));
                    list.add(new DefectModel("生产厂家", obj.getString("SCCJ")));
                    list.add(new DefectModel("部件类型", obj.getString("BJLX")));
                    list.add(new DefectModel("部件种类", obj.getString("BJZL")));
                    list.add(new DefectModel("是否发送调度", obj.getString("SFFSDD")));
                    list.add(new DefectModel("发现来源", obj.getString("FXLY")));
                    list.add(new DefectModel("发现人", obj.getString("FXR")));
                    list.add(new DefectModel("发现日期", toTimeStr2(obj.getString("FXRQ"))));
                    list.add(new DefectModel("发现班组", obj.getString("FXBZ")));
                    list.add(new DefectModel("发现人单位", obj.getString("FXRDW")));
                    list.add(new DefectModel("缺陷部位", obj.getString("QXBW")));
                    list.add(new DefectModel("缺陷描述", obj.getString("QXMS")));
                    list.add(new DefectModel("缺陷性质", obj.getString("QXXZ")));
                    list.add(new DefectModel("分类依据", obj.getString("FLYJ")));
                    list.add(new DefectModel("是否已消缺", obj.getString("SFXQ")));
                    list.add(new DefectModel("是否自行消缺", obj.getString("SFZXXQ")));
                    list.add(new DefectModel("状态量", obj.getString("ZTL")));
                    list.add(new DefectModel("扣分值", obj.getString("KFZ")));
                    list.add(new DefectModel("扣分说明", obj.getString("KFSM")));
                    list.add(new DefectModel("建议检修类别", obj.getString("JYJXLB")));
                    list.add(new DefectModel("建议检修时间", toTimeStr2(obj.getString("JYJXSJ"))));
                    list.add(new DefectModel("建议检修内容", obj.getString("JYJXNR")));
                    list.add(new DefectModel("检修建议", obj.getString("JXJY")));
                    list.add(new DefectModel("备注", obj.getString("BZ")));
                    list.add(new DefectModel("附件数量", obj.getString("FJSL")));
                    list.add(new DefectModel("消缺人", obj.getString("XQR")));
                    list.add(new DefectModel("消缺班组", obj.getString("XQBZ")));
                    list.add(new DefectModel("消缺单位", obj.getString("XQDW")));
                    list.add(new DefectModel("消缺时间", toTimeStr2(obj.getString("XQSJ"))));
                    list.add(new DefectModel("技术原因", obj.getString("JSYY")));
                    list.add(new DefectModel("责任原因", obj.getString("ZRYY")));
                    list.add(new DefectModel("工作负责人", obj.getString("GZFZR")));
                    list.add(new DefectModel("是否已延期消缺", obj.getString("SFYQXQ")));
                    list.add(new DefectModel("延期原因", obj.getString("YQYY")));
                    list.add(new DefectModel("处理结果", obj.getString("CLJG")));
                    list.add(new DefectModel("遗留问题", obj.getString("YLWT")));
                    list.add(new DefectModel("是否已验收", obj.getString("SFYYS")));
                    list.add(new DefectModel("验收是否合格", obj.getString("YSSFHG")));
                    list.add(new DefectModel("验收人", obj.getString("YSR")));
                    list.add(new DefectModel("验收班组", obj.getString("YSBZ")));
                    list.add(new DefectModel("验收时间", toTimeStr2(obj.getString("YSSJ"))));
                    list.add(new DefectModel("验收意见", obj.getString("YSYJ")));
                    list.add(new DefectModel("处理详情", obj.getString("CLXQ")));

                }


            }
        }
    }



    class MyAsynTask extends AsyncTask<String, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            DialogUtils.startProgressDialog(DefectDetailActivity.this,"正在加载");
            list=new ArrayList<>();

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if(list.size()<=0){
                Toast.makeText(DefectDetailActivity.this, "暂无缺陷信息", Toast.LENGTH_SHORT).show();
                finish();
            }
            tableLv.setAdapter(new DefectDetailAdapter(DefectDetailActivity.this, list));
            DialogUtils.stopProgressDialog();
        }
        @Override
        protected Void doInBackground(String... params) {
            if(type.equals("SD")){
                initDataSD();
            }else if(type.equals("BD")){
                initDataBD();
            }
            return null;
        }
    }


}
